import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/login'
  },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/home',
    name: 'home',
    meta:{
      needLogin:true
    },
    component: () => import(/* webpackChunkName: "home" */ '../views/Home.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

import $store from '../store'

router.beforeEach((to,from,next)=>{
  if(to.meta.needLogin){
    if($store.state.user.user){
      next()
    }else{
      router.push('/login')
    }
  }else{
    next()
  }
})

export default router
